const mysql = require('mysql');
const fs = require('fs');

const config = require('../../config/config');

// 创建链接池
const pool = mysql.createPool(config.DB)
const query = (sql) => {
    return new Promise((resolve, reject)=>{
        pool.getConnection((err, connection)=>{
            if(err){
                return resolve(err);
            }else{
                connection.query(sql, (err, results,)=>{
                    connection.release();      
                    if(err){
                        return reject(err);
                    }
                    return resolve(results);
                })
            }
        })
    })
};
pool.on('enqueue', function () {
    console.log('Waiting for available connection slot');
  });
// 创建链接
// const connection = mysql.createConnection(config.DB)
// connection.connect();
// const query = (sql) => {
//     return new Promise((resolve, reject)=>{

//         connection.query(sql, (err, results)=>{                    
//             if(err){
//                 return reject(err);
//             }
//             return resolve(results);
//         })
//     })
// };
module.exports = {query};